package com.example.demo.service;


import com.example.demo.entity.Account;
import com.example.demo.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("userDetailService")
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private AccountRepository accountRepository;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //Optional防止空指针异常
        Optional<Account> account = accountRepository.findById(s);
        if (account!=null){
            if (account.isPresent()){
//                HttpSession httpSession = null;
//                httpSession.setAttribute("username",account.get().getUsername());
                return account.get();
            }throw new UsernameNotFoundException("User Not Found!");
        }throw new UsernameNotFoundException("User Not Found!");

    }
}
